package com.HW;

import javax.swing.*;
import java.io.PrintStream;
import java.sql.SQLOutput;
import java.util.Arrays;
import java.util.Locale;

/**
 * @ClassName : TCarMonitor
 * @Author : kele
 * @Date: 2023/10/22 17:24
 * @Description : 需要打开多少监视器
 */
public class TCarMonitor {

    public static void main(String[] args) {

        handle("3 4", "0 0 0 0", "0 1 0 1", "0 0 0 0");
        handle("3 3", "0 0 0", "0 1 0", "0 0 0");

    }

    public static void handle(String coordinate, String... car) {

        String[] s = coordinate.split(" ");
        int x = Integer.parseInt(s[0]);
        int y = Integer.parseInt(s[1]);
        int[][] location = new int[x][y];

        for (int i = 0; i < x; i++) {
            String[] line = car[i].split(" ");
            for (int j = 0; j < line.length; j++) {
                location[i][j] = Integer.parseInt(line[j]);
            }
        }
        int num = 0;
        for (int i = 0; i < location.length; i++) {
            for (int j = 0; j < location[0].length; j++) {
                if (isMonitor(i, j, location)) {
                    num ++;
                }
            }
        }

        System.out.println(num);

    }

    public static boolean isMonitor(int x, int y, int[][] location) {

        int[][] actions = new int[][]{{0, 0}, {1, 0}, {0, 1}, {-1, 0}, {0, -1}};

        for (int[] action : actions) {

            int x_a = x + action[0];
            int y_a = y + action[1];

            if (x_a >= 0 && y_a >= 0 && x_a < location.length && y_a < location[0].length) {
                if (location[x_a][y_a] == 1) {
                    return true;
                }
            }
        }
        return false;
    }
}
